package com.library.dao;

import com.library.bean.Book;
import com.library.bean.PageInfo;
import com.library.mapper.BookInfoMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Repository
public class BookDao {

    @Resource
    private SqlSessionTemplate sqlSession;

    private final String namespace="com.library.dao.BookDao.";


    public ArrayList<Book> findByCurrentPage(int currentPage) throws IOException {
        PageInfo pageInfo=new PageInfo();
        //每页的数据量
        pageInfo.setSize(10);

        //总书籍量
        int totalCount=returnAllBookNum();
        pageInfo.setTotalPage(totalCount);


        //总页数
        int totalPage=(int)Math.ceil(totalCount/(double)pageInfo.getSize());
        pageInfo.setTotalPage(totalPage);

        //判断当前页是否合理，如果不合理，则设置为1或总页数，合理则返回当前页
        if (currentPage<1){
            pageInfo.setCurrentPage(1);
        }else if(currentPage>totalPage){
            pageInfo.setCurrentPage(totalPage);
        }else {
            pageInfo.setCurrentPage(currentPage);
        }

        int start=(pageInfo.getCurrentPage()-1)*pageInfo.getSize();
        //查询当前页面下所有的用户信息
        ArrayList<Book> list=findByCurrentPageStart(start,pageInfo.getSize());
        return list;




    }

    private ArrayList<Book> findByCurrentPageStart(@Param("start") int start,@Param("size")  int size) throws IOException {
        InputStream in= Resources.getResourceAsStream("config/springmvc/mybatis-config.xml");
        //获取SqlSessionFactoryBuilder对象
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder=new SqlSessionFactoryBuilder();
        //获取SqlSessionFactory对象
        SqlSessionFactory sqlSessionFactory=sqlSessionFactoryBuilder.build(in);
        //获取SqlSession对象
        SqlSession session=sqlSessionFactory.openSession(true);

        //获取mapper对象
        BookInfoMapper bookInfoMapper=session.getMapper(BookInfoMapper.class);
        ArrayList<Book> list=bookInfoMapper.findByCurrentPageStart(start,size);
        return list;


    }

    private int returnAllBookNum() throws IOException {
        InputStream in= Resources.getResourceAsStream("config/springmvc/mybatis-config.xml");
        //获取SqlSessionFactoryBuilder对象
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder=new SqlSessionFactoryBuilder();
        //获取SqlSessionFactory对象
        SqlSessionFactory sqlSessionFactory=sqlSessionFactoryBuilder.build(in);
        //获取SqlSession对象
        SqlSession session=sqlSessionFactory.openSession(true);

        //获取接口对象
        BookInfoMapper mapper=session.getMapper(BookInfoMapper.class);
        Integer num=mapper.returnAllBookNum();
        return num;


    }

    public PageInfo<Book> findByPage(int currentPage) throws IOException {
        PageInfo pageInfo=new PageInfo();
        //每页的数据量
        pageInfo.setSize(10);

        //总书籍量
        int totalCount=returnAllBookNum();
        pageInfo.setTotalPage(totalCount);


        //总页数
        int totalPage=(int)Math.ceil(totalCount/(double)pageInfo.getSize());
        pageInfo.setTotalPage(totalPage);

        //判断当前页是否合理，如果不合理，则设置为1或总页数，合理则返回当前页
        if (currentPage<1){
            pageInfo.setCurrentPage(1);
        }else if(currentPage>totalPage){
            pageInfo.setCurrentPage(totalPage);
        }else {
            pageInfo.setCurrentPage(currentPage);
        }

        int start=(pageInfo.getCurrentPage()-1)*pageInfo.getSize();
        //查询当前页面下所有的用户信息
        int pageinfo=pageInfo.getSize();
        List<Book> list=findByCurrentPageStart(start,pageinfo);
        return pageInfo;

    }

    public Book getBook(long bookId) {
        return sqlSession.selectOne(namespace+"getBook",bookId);
    }

    public int addBook(Book book) {
        return sqlSession.insert(namespace+"addBook",book);
    }

    public int deleteBook(Long bookId) {
        return sqlSession.insert(namespace+"deleteBook",bookId);
    }

    public int editBook(Book book) {
        return sqlSession.update(namespace+"editBook",book);

    }

    public int matchBook(String searchWord) throws IOException {
        //模糊查询的基本字符串定义
        String search="%"+searchWord+"%";

        //sqlsession简历与数据库的链接
        InputStream in= Resources.getResourceAsStream("config/springmvc/mybatis-config.xml");
        //获取SqlSessionFactoryBuilder对象
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder=new SqlSessionFactoryBuilder();
        //获取SqlSessionFactory对象
        SqlSessionFactory sqlSessionFactory=sqlSessionFactoryBuilder.build(in);
        //获取SqlSession对象
        SqlSession session=sqlSessionFactory.openSession(true);

        //获取接口对象
        BookInfoMapper mapper=session.getMapper(BookInfoMapper.class);
        Integer num=mapper.select(search);
        return num;

    }

    public ArrayList<Book> qureyBook(String searchWord) {
        //模糊查询的基本字符串定义
        String search="%"+searchWord+"%";

        List<Book> result=sqlSession.selectList(namespace+"qureyBook",search);
        return (ArrayList<Book>) result;


    }

    public int updateBooknum(Book book) {
        return sqlSession.update(namespace+"updateBooknum",book);
    }

    public int returnBooknum(Book book) {
        return sqlSession.update(namespace+"returnBooknum",book);
    }

    public ArrayList<Book> getAllBooks() {
        List<Book> reuslt=sqlSession.selectList(namespace+"getAllBooks");
        return (ArrayList<Book>) reuslt;

    }
}
